package com.toman.Adpter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.toman.Entity.MyBeltline_Entity;
import com.toman.JavaBean.BeltlineInspectEntity;
import com.toman.JavaBean.DeviceCheckRecordEntity;
import com.toman.JavaBean.TerminalWarmUpDto;
import com.toman.MyApplication;
import com.toman.R;
import com.toman.Util.TimeUtil;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.widget.SpaceItemDecoration;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by  MyExpandableListViewAdapter on Administrator on 2017/11/27 on 10:34.
 */

public class MyExpandableListPreviewHotAdapter extends BaseExpandableListAdapter {
    public interface ActionClickListener {
        public void onChangeClick(boolean b);
        //点击变化监听

    }

    private RemoteStart_adpter gridViewAdapter = null;
    private  ActionClickListener actionClickListener;
    public void setActionClickListener(ActionClickListener l) {
        actionClickListener = l;
    }

    private Context mContext;

    /**
     * 每个分组的名字的集合
     */
    private ArrayList<String> groupList;

    /**
     * 所有分组的所有子项的 GridView 数据集合
     */
    private ArrayList<ArrayList<TerminalWarmUpDto>> itemList;

    private RecyclerView recyclerView;
    CommonAdapter<TerminalWarmUpDto> adapter;
    CommonAdapter<TerminalWarmUpDto>[] adapters;
    private ArrayList<TerminalWarmUpDto> list = new ArrayList<TerminalWarmUpDto>();

    public     CommonAdapter<BeltlineInspectEntity> adapterHot;

    public ArrayList<TerminalWarmUpDto> getSelectedList() {
        return list;
    }

    public void setList(ArrayList<TerminalWarmUpDto> list) {
        this.list = list;
    }

    public void selectAll(){
        for (TerminalWarmUpDto entity : list) {
            entity.setSelected(true);
        }
        notifyDataSetChanged();
    }

    public void selectContrary(){
        for (TerminalWarmUpDto entity : list) {
            entity.setSelected(!entity.isSelected());
        }
        notifyDataSetChanged();
    }

    public MyExpandableListPreviewHotAdapter(Context context, ArrayList<String> groupList, ArrayList<ArrayList<TerminalWarmUpDto>> itemList) {
        mContext = context;
        this.groupList = groupList;
        this.itemList = itemList;
        refreshList();

    }

    public void refreshList() {
        list.clear();
        adapters=new CommonAdapter[groupList.size()];
        for (int i=0;i<itemList.size();i++){
            list.addAll(itemList.get(i));
        }
    }


    @Override
    public int getGroupCount() {
        return groupList.size();
    }

    //itemList.get(groupPosition).size()
    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return itemList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (null == convertView) {

            convertView = View.inflate(mContext, R.layout.expandablelist_preview_hot, null);
        }
        ImageView ivGroup = (ImageView) convertView.findViewById(R.id.iv_group);
        TextView tvGroup = (TextView) convertView.findViewById(R.id.tv_group);
        ImageView ivColor = (ImageView) convertView.findViewById(R.id.iv_color);
        TextView tv_msg = (TextView) convertView.findViewById(R.id.tv_msg);

        switch (groupList.get(groupPosition)){
    case "报警":
        ivColor.setImageDrawable(mContext.getResources().getDrawable(R.drawable.circle_hot_110));
        tv_msg.setText("已有产量,必须至车间确认才能关闭预热,以防出现人身事故 ");
        // 设置分组组名
        try {
            tvGroup.setText(groupList.get(groupPosition) + "(" + itemList.get(groupPosition).size() + ")");
        } catch (Exception e) {
            e.printStackTrace();
            tvGroup.setText(groupList.get(groupPosition) + "(" + "0" + ")");
        }
tv_msg.setVisibility(View.VISIBLE);
        break;
    case "待检查":
        ivColor.setImageDrawable(mContext.getResources().getDrawable(R.drawable.circle_hot_unkown));
        tv_msg.setText("请排查(是否陀曼设备),无状态原因请填写到终端信息");
        tv_msg.setVisibility(View.VISIBLE);
        // 设置分组组名
        try {
            tvGroup.setText(groupList.get(groupPosition) + "(" + itemList.get(groupPosition).size() + ")");
        } catch (Exception e) {
            e.printStackTrace();
            tvGroup.setText(groupList.get(groupPosition) + "(" + "0" + ")");
        }
        break;
    case "正常预热（待机）":
        ivColor.setImageDrawable(mContext.getResources().getDrawable(R.drawable.circle_hot_oil));
        tv_msg.setVisibility(View.GONE);
        // 设置分组组名
        try {
            tvGroup.setText("正常预热" + "(待机/" + itemList.get(groupPosition).size() + ")");
        } catch (Exception e) {
            e.printStackTrace();
            tvGroup.setText(groupList.get(groupPosition) + "(" + "0" + ")");
        }
        break;
    case "正常预热（空转）":
        ivColor.setImageDrawable(mContext.getResources().getDrawable(R.drawable.circle_hot_normal));
        tv_msg.setVisibility(View.GONE);
        try {
            tvGroup.setText("正常预热" + "(空转/" + itemList.get(groupPosition).size() + ")");
        } catch (Exception e) {
            e.printStackTrace();
            tvGroup.setText(groupList.get(groupPosition) + "(" + "0" + ")");
        }
        break;

}
        // 如果是展开状态，就显示展开的箭头，否则，显示折叠的箭头
        if (isExpanded) {
            ivGroup.setImageResource(R.mipmap.upward);
        } else {
            ivGroup.setImageResource(R.mipmap.down);
        }

        return convertView;
    }


    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        convertView = View.inflate(mContext, R.layout.expandablelist_recycle, null);
        recyclerView = (RecyclerView) convertView;
        GridLayoutManager mgr = new GridLayoutManager(mContext, 3);
        recyclerView.setLayoutManager(mgr);
        recyclerView.addItemDecoration(new SpaceItemDecoration(20, 3));

        // 因为 convertView 的布局就是一个 GridView
        // 所以可以向下转型为 GridView
        if (adapters[groupPosition]==null){
            adapters[groupPosition]= initAdapter(itemList.get(groupPosition));

        }
        else
        {
            recyclerView.setAdapter(adapters[groupPosition]);
            adapters[groupPosition].setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                    RelativeLayout rl_select = view.findViewById(R.id.rl_select);


                        ViewUtil.hideView(rl_select);
                        TerminalWarmUpDto bean=itemList.get(groupPosition).get(position);
                        bean.setSelected(!bean.isSelected());

                        if (actionClickListener != null) {
                            actionClickListener.onChangeClick(true);
                        }



                }

                @Override
                public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                    return false;
                }
            });

            adapters[groupPosition].notifyDataSetChanged();}
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }






    private CommonAdapter<TerminalWarmUpDto> initAdapter(List<TerminalWarmUpDto> list) {
        CommonAdapter<TerminalWarmUpDto> commonAdapter=   new CommonAdapter<TerminalWarmUpDto>(mContext, R.layout.item_preview_hot_card, list) {
            @Override
            protected void convert(ViewHolder holder, TerminalWarmUpDto item, final int position) {
                CardView cv = holder.getView(R.id.cv);
                RelativeLayout rl_select = holder.getView(R.id.rl_select);

                holder.setText(R.id.tv_terminal_num, item.getTerminalNum());
                holder.setText(R.id.tv_beltline, item.getBeltlineName());
                if (item.getDeviceName()==null){
                    holder.getView(R.id.tv_output).setVisibility(View.GONE);

                }
                else {
                    holder.setText(R.id.tv_output, item.getDeviceName() + "");
                }
                if (item.isSelected()){
                    rl_select.setVisibility(View.VISIBLE);
                }
                else {
                    rl_select.setVisibility(View.GONE);
                }
                ImageView iv_status = holder.getView(R.id.iv_status);
//                if (item.getTerminalOnlineStatus() == 1) {
//                    iv_status.setImageResource(R.mipmap.cxxj_icon_online);
//                } else {
//                    iv_status.setImageResource(R.mipmap.cxxj_icon_offline);
//                }
 holder.setText(R.id.tv_time,"已预热"+TimeUtil.convert2min(item.getWarmUpTime())) ;
                if (item.getWarmUpIngStatus()==null)
                        {
                                cv.setCardBackgroundColor(mContext.getResources().getColor(R.color.hot_unkown));
                        }
else {
                    switch (item.getWarmUpIngStatus()) {
                        case "3":
                            cv.setCardBackgroundColor(mContext.getResources().getColor(R.color.hot_normal));

                            break;
                        case "1":
                            cv.setCardBackgroundColor(mContext.getResources().getColor(R.color.hot_oil));
                            break;
                        case "2":
                            cv.setCardBackgroundColor(mContext.getResources().getColor(R.color.hot_110));
                            break;
                        case "0":
                            cv.setCardBackgroundColor(mContext.getResources().getColor(R.color.hot_unkown));
                            break;
                    }
                }
            }
        };
        return commonAdapter;
    }

}
